// component/pickerDataTime/pickerDataTime.js
import dayjs from "../../utils/dayjs.min"
Component({
  options: {
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    minute: Boolean,
    second: Boolean,
    valueFormat: {
      type: String,
      value: 'YYYY-MM-DD HH:mm:ss'
    },
    format: {
      type: String,
      value: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  /**
     * 生命周期
     * 在组件实例进入页面节点树时执行
     */
  lifetimes: {
    attached: function () {
      this.infoEvent()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    pickerList: [],
    value: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    infoEvent() {
      const yearArr = []; // 年
      const monthArr = []; // 月
      const dayArr = [] // 日
      const hourArr = [] // 小时
      const minuteArr = [] // 分钟
      const secondArr = [] // 秒
      const date = new Date(), year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(), endData = new Date(year, month, 0).getDate(), hour = date.getHours(), minute = date.getMinutes(), second = date.getSeconds()
      for (let index = year; index < year + 5; index++) yearArr.push(`${index}年`);
      for (let index = 1; index <= 12; index++) monthArr.push(`${index}月`);
      for (let index = 1; index <= endData; index++) dayArr.push(`${index}日`);
      for (let index = 1; index <= 24; index++) hourArr.push(`${index}时`);
      for (let index = 1; index <= 60; index++) minuteArr.push(`${index}分`), secondArr.push(`${index}秒`);
      // 当前时间下标
      let yearIndex = 0, monthIndex = 0, dayIndex = 0, hourIndex = 0, minuteIndex = 0, secondIndex = 0;
      // 年 月 日
      yearIndex = yearArr.findIndex(item => item == `${year}年`)
      monthIndex = monthArr.findIndex(item => item == `${month}月`)
      dayIndex = dayArr.findIndex(item => item == `${day}日`)
      // 时 分 秒
      hourIndex = hourArr.findIndex(item => item == `${hour}时`)
      minuteIndex = minuteArr.findIndex(item => item == `${minute}分`)
      secondIndex = secondArr.findIndex(item => item == `${second}秒`)
      const format = this.data.format
      const [formatDate, formatTime] = format.split(' ')
      const dateOptions = { "YYYY": yearArr, "MM": monthArr, "DD": dayArr, "HH": hourArr, "mm": minuteArr, "ss": secondArr }
      const valueIndex = { "YYYY": yearIndex, "MM": monthIndex, "DD": dayIndex, "HH": hourIndex, "mm": minuteIndex, "ss": secondIndex }
      let value = [], list = []
      formatDate?.split('-')?.forEach(item => (list.push(dateOptions[item]), value.push(valueIndex[item])))
      formatTime?.split(':')?.forEach(item => (list.push(dateOptions[item]), value.push(valueIndex[item])))
      this.setData({ pickerList: list, value })
    },

    pickerChange(event) {
      const { pickerList } = this.data
      const [yearArr, monthArr, dayArr, hourArr, minuteArr, secondArr] = pickerList
      const [yearIndex, monthIndex, dayIndex, hourIndex, minuteIndex, secondIndex] = event.detail.value
      const year = yearArr[yearIndex]; // 年
      const month = monthArr[monthIndex]; // 月
      const day = dayArr[dayIndex] // 日
      const hour = hourArr ? hourArr[hourIndex || 0] : null // 小时
      const minute = minuteArr ? minuteArr[minuteIndex || 0] : null // 分钟
      const second = secondArr ? secondArr[secondIndex || 0] : null // 秒
      const timeStr = `${parseInt(year)}-${parseInt(month)}-${parseInt(day)} ${parseInt(hour || 0)}:${parseInt(minute || 0)}:${parseInt(second || 0)}`
      this.triggerEvent("change", dayjs(timeStr).format(this.data.valueFormat))
    },
    columnChange(event) {
      const { column, value } = event.detail;
      const { pickerList, value: dataValue } = this.data
      const [yearArr, monthArr, dayArr, hourArr, minuteArr, secondArr] = pickerList
      dataValue[column] = value
      let [yearIndex, monthIndex, dayIndex, hourIndex, minuteIndex, secondIndex] = dataValue
      if (column != 0 && column != 1) return
      let dayList = []
      const year = parseInt(yearArr[yearIndex])
      const month = parseInt(monthArr[monthIndex])
      const endData = new Date(year, month, 0).getDate()
      for (let index = 1; index <= endData; index++) dayList.push(`${index}日`);
      let list = []
      const format = this.data.format
      const [formatDate, formatTime] = format.split(' ')
      const dateOptions = { "YYYY": yearArr, "MM": monthArr, "DD": dayList, "HH": hourArr, "mm": minuteArr, "ss": secondArr }
      formatDate?.split('-')?.forEach(item => list.push(dateOptions[item]))
      formatTime?.split(':')?.forEach(item => list.push(dateOptions[item]))
      this.setData({ pickerList: list })
    }
  }
})
